package cn.gong.jizhang.ui;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.View;
import cn.gong.jizhang.config.AppParameters;
import cn.gong.jizhang.config.Configs;
import cn.gong.jizhang.config.PhoneParameters;
import cn.gong.jizhang.enumparams.DbTaskType;
import cn.gong.jizhang.manager.ContextManager;
import cn.gong.jizhang.service.MainService;
import cn.gong.jizhang.ui.base.BaseAct;
import cn.gong.jizhang.utils.Md5Utils;

public class LogoAct extends BaseAct {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (Configs.IS_TEXT_LAYOUT) {
			setContentView(R.layout.logo);
		} else {
			setLayout(R.layout.logo);
			// 保存上线文对象
			saveContext();
			// 保存手机参数
			savePhoneArg();
			// 启动服务
			startService();
			// 应用参数获取
			appVersion();
			// 开始主界面
			startMain();
		}
	}

	private void saveContext() {
		ContextManager.getInstance().setContext(this);
	}

	@Override
	protected void setClickListener() {
	}

	private void startMain() {
		final Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				timer.cancel();
				Intent intent = new Intent(LogoAct.this, RecordAct.class);
				// Intent intent = new Intent(LogoAct.this, LoginAct.class);
				openActForNew(intent);
				finish();
			}
		}, 1000);
	}

	private void savePhoneArg() {
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		// 手机分辨率
		PhoneParameters.screenWidth = metric.widthPixels; // 屏幕宽度（像素）
		PhoneParameters.screenHight = metric.heightPixels; // 屏幕宽度（像素）
		PhoneParameters.density = metric.density; // 屏幕密度（0.75 / 1.0 / 1.5）
		// 手机品牌
		PhoneParameters.manufacturer = Build.MANUFACTURER;
		// 手机型号
		PhoneParameters.model = Build.MODEL;
		// 手机系统版本号
		PhoneParameters.release = Build.VERSION.RELEASE;
		PhoneParameters.sdk_version = android.os.Build.VERSION.SDK_INT;
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) && Configs.IS_CACHE_SDCARD) {
			// 存在sdcard
			File sdcardDir = Environment.getExternalStorageDirectory();
			// download目录
			PhoneParameters.downloadDir = sdcardDir.getAbsolutePath() + "/igeifu/download/";
			// 图片缓存
			PhoneParameters.imageFileDir = sdcardDir.getAbsolutePath() + "/igeifu/file/image/";
			// 声音缓存
			PhoneParameters.soundFileDir = sdcardDir.getAbsolutePath() + "/igeifu/file/sound/";
		} else {
			// download目录
			PhoneParameters.downloadDir = getFilesDir().getAbsolutePath() + "/download/";
			// 图片缓存
			PhoneParameters.imageFileDir = getFilesDir().getAbsolutePath() + "/image/";
			// 声音缓存
			PhoneParameters.soundFileDir = getFilesDir().getAbsolutePath() + "/sound/";
		}
		// app数据缓存
		PhoneParameters.appDataDir = getCacheDir().getAbsolutePath() + "/" + Md5Utils.MD5("app") + "/";
		// 好友数据缓存
		PhoneParameters.friendsDataDir = getCacheDir().getAbsolutePath() + "/" + Md5Utils.MD5("friends") + "/";

	}

	private void startService() {
		if (!MainService.serviceState) {
			Intent intent = new Intent(this, MainService.class);
			startService(intent);
		}
	}

	private void appVersion() {
		PackageInfo info = null;
		try {
			info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
		} catch (NameNotFoundException e) {
			exception(e);
		}
		AppParameters.VERSION_CODE = info.versionCode;
		AppParameters.VERSION_NAME = info.versionName;
	}

	@Override
	public void onClick(View arg0) {
	}

	@Override
	protected void clearDate() {
	}

	@Override
	protected void recoveryDate() {
	}

	@Override
	public void dbTaskFinish(DbTaskType tasktype, Object data) {
	}

}
